from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from . import serializers
# Create your views here.
class add_role(APIView):
    def post(self,request):
        name =request.data['name']
        Role.objects.create(
            name=name
        )
        return Response({
            'code':200,
            'mm':'成功'
        })

class add_user(APIView):
    def post(self,request):
        name = request.data['name']
        img = request.FILES['img']
        role = request.data['role']
        roles = Role.objects.filter(id=role).first()
        if User.objects.filter(name=name).first():
            return Response({
                'code':201,
                'message':'重复'
            })
        else:
            User.objects.create(
                name=name,
                img=img,
                role_id=roles
            )
            return Response({
                'code':200,
                'message':'成功'
            })

class show_role(APIView):
    def get(self,request):
        role = Role.objects.all()
        roles = serializers.role_serializers(instance=role,many=True)
        return Response({
            'code':200,
            'roles':roles.data
        })

class index(APIView):
    def get(self,request):
        user = User.objects.all()
        users = serializers.user_serializers(instance=user,many=True)
        return Response({
            'code':200,
            'users':users.data
        })


from django.core import paginator
class show_pa(APIView):
    def post(self,request):
        c = User.objects.all()
        try:
            p = int(request.data['p'])
        except:
            p  = 1
        tiao = paginator.Paginator(c,2)
        pa = tiao.page(p)
        cc = serializers.user_serializers(instance=pa,many=True)
        return Response({
            'code':200,
            'message':cc.data,
            'list':list(tiao.page_range)
        })

class xiang(APIView):
    def post(self,request):
        id = request.data['id']
        user = User.objects.filter(pk=id)
        users = serializers.user_serializers(instance=user,many=True)
        return Response({
            'code':200,
            'users':users.data
        })

class counts(APIView):
    def get(self,request):
        a = cishu.objects.get(pk=1)
        a.counts +=1
        a.save()
        # aa = countr.counts
        return Response({
            'code':200,
            'a':a.counts
        })

class delete(APIView):
    def post(self,request):
        id = request.data['id']
        User.objects.get(pk=id).delete()
        return Response({
            'code':200,
            'message':'成功'
        })

class update(APIView):
    def post(self,request):
        id = request.data['id']
        name = request.data['name']
        good = User.objects.get(pk=id)
        good.name=name
        good.save()
        return Response({
            'code':200,
            'message':'成功'
        })